<?php
class User_model extends CI_Model {
		
	private $salt = 'r4nd0m';
    
    public $ADMIN = 0;
    public $ATENDENTE = 1;
	
	public function get ($id = false){
	    if ($id) $this->db->where('id', $id);
	    $this->db->order_by('nome', 'asc');
	    $get = $this->db->get('usuario');
	    if($id) return $get->row_array();
	    if($get->num_rows > 0) return $get->result_array();
	    return array();
	}
	
	public function create($data){
		// $data['password'] = sha1($data['password'].$this->salt);
		return $this->db->insert('usuario', $data);
	}
	
	public function update($id, $data){
	    /*if(isset($data['password'])){
	    	$data['password'] = sha1($data['password'].$this->salt);
	    }*/
	    $this->db->where('id', $id);
	    $update = $this->db->update('usuario', $data);
	    return $update;
	}
	
	public function delete($id){
	    $this->db->where('id', $id);
	    $this->db->delete('usuario');
	}
	
	public function validate($username, $password){
	    // $this->db->where('username', $username)->where('password', sha1($password.$this->salt));
	    $this->db->where('username', $username)->where('password', $password);
	    $get = $this->db->get('usuario');
	 	if($get->num_rows > 0) return $get->row_array();
	    return array();
	}
}
